function Ro() {
    this.request = {
        Ro : {
            name : null,
            method : null,
            params : []
        }
    };

    this.set = function(attribute, value) {
		eval("this.request.Ro." + attribute + "='" + value + "'");
		return this;
    };

	this.addParam = function(param) {
		this.request.Ro.params.push(param);
		return this;
	},

    this.serialize = function() {
		return $.param(this.request);
    };

    return this;
}

var hmData = {
	init : function() {
		var lang = $.getUrlVar('lang');
		if ($.inArray(lang, conf.availableLangs) == -1) {
			lang = conf.defaultLang;
		}
		i18n.lang = lang;
		
		var testResponse = {
			sections : [{
				sectionId:1, 
				sectionName:'dsafsdf', 
				items:[{
					itemId:2, 
					itemName:"Engagement ring", 
					description: "", 
					attributes: {
						14: {
							attrName:"Cost", 
							attrType:2
						}
					}
				}]
			}]
		};
		
		conf.sectionForm.section(testResponse);
		/*
		conf.gateway += '?' + $.param({
			uId: $.getUrlVar('uId'),
			uSig: $.getUrlVar('uSig'),
			lang: lang
		});
		var locale = eval('conf.locale.' + lang);
		$.datepicker.regional[locale].dateFormat = "DD, d MM, yy";
    	$.datepicker.setDefaults( $.datepicker.regional[locale] );
    	*/

		/*
		var ro = new Ro().set('name', 'PlannerWeddingService').set('method', 'load');
        $.ajax({
			url : conf.gateway,
			mimeType: 'script',
            data : ro.serialize(),
            success : function(data) {
				conf.user.userData.items      = data.response.uItem;
				conf.user.userData.attributes = data.response.uAttr;
				conf.user.info                = data.response.uWed;
				conf.sectionForm.section({sections:data.response.gConf});
				conf.userItemForm.itemform('refresh');
				$('#application').app('loadUserInfoForm');
				helperStatus.init();
            }
        });
        */
    },

	update : function(print) {
		var ro = new Ro()
			.set('name', 'PlannerWeddingService')
			.set('method', 'update')
			.addParam(conf.user.userData.items.length?conf.user.userData.items:'')
			.addParam(conf.user.userData.attributes);
		$.ajax({
			url : conf.gateway,
			data : ro.serialize(),
			type: 'POST',
            success : function(data) {
				helperStatus.init();
				if (print) {
					hmData.getPrint();
				}
            }
        });
	},

	getItemDescription : function(item) {
		this.getTippDlg(item.itemName, item.description);
		var ro = new Ro()
			.set('name', 'PlannerWeddingService')
			.set('method', 'getItemDescription')
			.addParam(item.descriptionId);
		$.ajax({
            data : ro.serialize(),
            success : function(response) {
				conf.itemInfoDialog
					.text(response.response)
					.dialog('option', 'title', item.itemName)
					.dialog('open');
            }
        });
	},

	getPrint : function() {
		var ro  = new Ro()
			.set('name', 'PlannerWeddingService')
			.set('method', 'getUserItemsPreview')
			.addParam({
				order : $.cookie("userItemsOrder"),
				dateFormat : $.datepicker.regional[eval('conf.locale.' + i18n.lang)].dateFormat,
				budget : conf.budgetCounter.text()
			});

		$.ajax({
			url : conf.gateway,
			data : ro.serialize(),
            success : function(data) {
				$('#org-preview').html(data.response);
				window.print();
            }
        });
	},

	updateUserInfo : function() {
		var ro  = new Ro()
			.set('name', 'PlannerWeddingService')
			.set('method', 'updateWeddingInfo')
			.addParam(conf.user.info);

		$.ajax({
			url : conf.gateway,
			data : ro.serialize(),
            success : function(data) {
				$('#application').app('loadUserInfo');
            }
        });
	},

	getItemTipp : function(item) {
		var self = this;
		if (conf.tipps[item.itemId]) {
			self.getTippDlg(item.itemName, conf.tipps[item.itemId]);
			return;
		}

		var ro = new Ro()
			.set('name', 'PlannerWeddingService')
			.set('method', 'getItemTipp')
			.addParam(item.itemId);
		$.ajax({
			url : conf.gateway,
			data : ro.serialize(),
            success : function(response) {
            	conf.tipps[item.itemId] = response.response ? response.response : 'Для данного элемента советы отсутствуют';
				self.getTippDlg(item.itemName, conf.tipps[item.itemId]);
            }
        });
	},

	getTippDlg : function(title, text) {
		conf.itemInfoDialog
			.text(text)
			.dialog('option', 'title', title)
			.dialog('open');
	},

	getAuthFailedDlg : function() {
		conf.itemInfoDialog
			.text('You are not authorized to see application content')
			.dialog('option', 'title', 'Authorization failed!')
			.dialog('open');
	}
};